package com.vive.book;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigation;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.model.PropertyModel;

import java.util.ArrayList;
import java.util.List;

public class BookListPage extends WebPage {

    private static List books = new ArrayList();

    static {
        for (int i = 1; i < 100; i++) {
            Book book = new Book();
            book.setId(i);
            book.setAuthor("author"+i);
            book.setTitle("title"+i);

            books.add(book);



        }
    }

    public BookListPage(){
        super();

//        add(new ListView("books",books) {
//
//            @Override
//            protected void populateItem(ListItem item) {
//                Book book = (Book) item.getModelObject();
//                item.add(new Label("id",Integer.toString(book.getId())));
//                item.add(new Label("title",book.getTitle()));
//                item.add(new Label("author",book.getAuthor()));
//            }
//        });

        Form form = new Form("formBooks"){
            protected void onSubmit(){

            }
        };

        add(form);

        final PageableListView listView = new PageableListView("books",books,10) {
            @Override
            protected void populateItem(ListItem item) {
                Book book = (Book) item.getModelObject();
                item.add(new CheckBox("selected", new PropertyModel(book,"selected")));
                item.add(new Label("id",Integer.toString(book.getId())));
                item.add(new Label("title",book.getTitle()));
                item.add(new Label("author",book.getAuthor()));
                item.add(new Link("edit") {

                    @Override
                    public void onClick() {
                        System.out.println("跳转编辑页面");
                    }
                });

                // 调试输出
                System.out.println("Added book: " + book.getId() + " - " + book.getTitle() + " - " + book.getAuthor());
            }
        };

        add(form);
        form.add(listView);
        form.add(new PagingNavigator("navigator", listView));
    }
}
